package com.warm.netty;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadConfig {
    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程池数量
        threadPoolTaskExecutor.setCorePoolSize(200);
        // 最大线程数量
        threadPoolTaskExecutor.setMaxPoolSize(600);
        // 线程队列
        threadPoolTaskExecutor.setQueueCapacity(0);
        // 非核心线程空闲等待时间
        threadPoolTaskExecutor.setKeepAliveSeconds(400);
        // 线程池拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 优雅降级
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 初始化
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}
